#ifndef UTI_BROADCAST_OPEN_H_
#define UTI_BROADCAST_OPEN_H_
#pragma once

#include "sys_config.h"
#include "uti_bar.h"
#include "uti_barseries.h"
using namespace std;
//
//






class BroadcastOpen
{
public:
	
	BroadcastOpen(BarInfo* pbar);

	~BroadcastOpen();

	bool Register(Bartype type, HANDLE* phan/*out*/, vector<BarSeries*>* pvbarser/*out*/);

	bool UnRegister(Bartype type);

	BarData* GetDriveBar(Bartype type)
	{
		unsigned int index = 0;
		if (type == tick)
			index = 0;


		BarData* pbardata = m_shmarr[index].pmq_barser + (m_pdrivemqindex_client[index]++);

		//printf("[GetDriveBar] %s, LastPrice:%0.2f, index: %d, %s\n", m_barinfo.instru, pbardata->LastPrice, m_pdrivemqindex_client[index] - 1, m_matainsemrec[index].semname);

		//pantheios::log_NOTICE("[GB] ", m_barinfo.instru, ", lp:", panreal(pbardata->LastPrice), ", index:", panint(m_pdrivemqindex_client[index] - 1), ", ", m_matainsemrec[index].semname);

		return pbardata;

	};
private:

	bool OpenSharedMQ(unsigned int index, unsigned int sersize, char* suffix);

	void DestroySharedMQ(unsigned int index);

	bool OpenSharedSemRecord(unsigned int index, char* suffix);

	void DestroySharedSemRecord(unsigned int index);

	bool OpenSharedMutex();

	void DestroySharedMutex();

	bool DestroySharedsema(unsigned int index);

	bool InitSharedsema(unsigned int index);

	bool CalculateSemName(unsigned int index);

	void GetCharName(const char* pname, char* pchar);

	bool GetSecurityAttributes(SECURITY_ATTRIBUTES &securityAttributes);

public:

	BarInfo m_barinfo;


private:
	typedef struct semrecord{
		semrecord()
		{
			han = NULL;
			bactive = false;
			memset(semname, 0, sizeof(semname));
		}
		HANDLE han;
		bool bactive;
		char semname[50];
	}semrecord;

	typedef struct SharedMemory
	{
		HANDLE hMapFile_barser;
		PVOID pView_barser;
		unsigned int* pindex_mq;
		BarData* pmq_barser;


		HANDLE hMapFile_semrec;
		PVOID pView_semrec;
		unsigned int* psize_semrec;

		semrecord* pmq_semrec;
	}SharedMemory;

	HANDLE m_mutex;

	SharedMemory m_shmarr[4];


	semrecord m_matainsemrec[4];

private:

	unsigned int m_pdrivemqindex_client[4];
	

	
};



#endif